<template>
  <div>
    <nav :class="[{ active: active || dropdownOpen }]">
      <div class="container">
        <div @click="$router.push('/')" class="left">
          {{ blogger.nickname || "- -" }}
        </div>
        <div class="right">
          <template v-if="!isRetract">
            <template v-for="(item, index) in navList">
              <div
                @click="toPage(item)"
                v-if="!item.childrens"
                :key="index"
                :class="[
                  'item',
                  'item-inactive',
                  { 'item-active': storePath == item.path },
                ]"
              >
                <div class="title">{{ item.name }}</div>
              </div>
              <div v-else :key="index" class="item drop-down">
                <div class="title">{{ item.name }}</div>
                <ul>
                  <li
                    @click="toPage(v)"
                    v-for="(v, i) in item.childrens"
                    :key="i"
                    :class="[
                      'item-inactive',
                      { 'item-active': storePath == v.path },
                    ]"
                  >
                    {{ v.name }}
                  </li>
                </ul>
              </div>
            </template>
          </template>
          <!-- 图标 -->
          <div
            @click="dropdownOpen = !dropdownOpen"
            v-else
            :class="['more-icon', { 'more-open': dropdownOpen }]"
          >
            <div></div>
            <div></div>
            <div></div>
          </div>
        </div>
      </div>
      <div :class="['more-dropdown', { 'more-dropdown-open': dropdownOpen }]">
        <div
          @click="toPage(item)"
          v-for="(item, index) in dropdownList"
          :key="index"
          :class="[
            'item',
            'item-inactive',
            { 'item-active': storePath == item.path },
          ]"
        >
          {{ item.name }}
        </div>
      </div>
    </nav>
  </div>
</template>

<script>
export default {
  name: "WorkspaceJsonNavBar",
  props: {
    active: {
      default: false,
    },
  },
  //   item-active
  data() {
    return {
      navList: [
        {
          name: " 🏠首页",
          path: "/",
          id: "/",
        },
        {
          name: "✍️一点笔记",
          path: "/article",
          id: "article",
        },
        {
          name: "🏖️茶余饭后",
          path: "/life",
          id: "life",
        },
        {
          name: "🖼️关于我",
          path: "/aboutMe",
          id: "aboutMe",
        },
        {
          name: "🍷我的朋友",
          path: "/friend",
          id: "friend",
        },
        {
          name: "🍎这里还有",
          childrens: [
            {
              name: "📚我的书单",
              path: "/booklist",
              id: "booklist",
            },
            {
              name: "🍟开源&项目",
              path: "/project",
              id: "project",
            },
            // {
            //   name: "🎁私信箱子",
            // },
          ],
        },
      ],
      navWidth: 0,
      dropdownOpen: false,
      isRetract: false,
    };
  },

  computed: {
    storePath() {
      return this.$route.path;
    },
    blogger() {
      return this.$store.state.blogger;
    },
    dropdownList() {
      let list = [];
      this.navList.forEach((i) => {
        if (i.childrens && i.childrens[0]) {
          list = [...list, ...i.childrens];
        } else {
          list.push(i);
        }
      });
      return list;
    },
  },
  mounted() {
    window.onresize = this.onScreen;
    this.$nextTick(() => {
      this.navWidth = document.querySelector("nav .right").clientWidth;
      this.onScreen();
    });
  },

  methods: {
    onScreen() {
      let clientWidth = document.body.clientWidth;
      if (clientWidth < this.navWidth + 150) {
        this.isRetract = true;
      } else {
        this.dropdownOpen = false;
        this.isRetract = false;
      }
    },
    toPage(item) {
      if (item.path) this.$router.push(item.path);
      this.dropdownOpen = false;
    },
  },
};
</script>

<style lang="scss" scoped>
nav {
  width: 100%;
  // height: 60px;
  position: fixed;
  transition: 0.3s ease-out;
  // box-shadow: 0 0.5rem 1rem rgba(51, 52, 53, 0.3);
  // border: 1px solid rgba(255, 255, 255, 0.05);
  z-index: 999;
  cursor: pointer;
  user-select: none;
}
nav * {
  transition: all 0s;
}
.container {
  max-width: 95%;
  height: 60px;
  padding: 0 1%;
  margin: auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #fff;
  font-weight: 600;
  .right {
    display: flex;
    .item {
      height: 60px;
      line-height: 60px;
      margin-left: 20px;
      font-size: 15px;
      letter-spacing: 1px;
      &::before {
        bottom: 18px !important;
      }
    }
    // 二级导航
    .drop-down {
      overflow: hidden;
      position: relative;
      .title {
        position: relative;
        display: flex;
        align-items: center;
        &::after {
          display: inline-block;
          margin-left: 0.255em;
          vertical-align: 0.255em;
          content: "";
          border-top: 0.3em solid;
          border-right: 0.3em solid transparent;
          border-bottom: 0;
          border-left: 0.3em solid transparent;
        }
      }
      ul {
        opacity: 0;
        background: #fff;
        color: #333;
        border-radius: 5px;
        transition: all 0.2s;
        transform: scale(1, 0.6);
        transform-origin: top;
        overflow: hidden;
        padding: 10px;
        li {
          width: fit-content;
          margin: auto;
          line-height: 40px;
          padding: 2px 10px;
          position: relative;
        }
      }
      &:hover {
        overflow: visible;
      }
      &:hover ul {
        opacity: 1;
        transform: scale(1, 1);
      }
    }
  }
  // 打开下面icon
  .more-icon {
    div {
      width: 29px;
      height: 4px;
      background-color: #fff;
      margin: 4px 0;
      border-radius: 5px;
      transition: all 0.2s;
      transform-origin: center;
    }
  }
  .more-open {
    div {
      &:nth-child(1) {
        margin: 0;
        transform: rotate(45deg) translate(5px, 4px);
      }
      &:nth-child(2) {
        opacity: 0;
      }
      &:nth-child(3) {
        margin: 0;
        transform: rotate(-45deg) translate(7px, -6px);
      }
    }
  }
}
// 下拉面板
.more-dropdown {
  max-width: 95%;
  margin: auto;
  height: 0;
  overflow: hidden;
  transition: all 0.2s;
  .item {
    width: fit-content;
    height: 60px;
    line-height: 60px;
    text-align: center;
    color: #fff;
    font-weight: 600;
    border-radius: 5px;
    margin: auto;
  }
}
.more-dropdown-open {
  height: 420px;
}

// 添加背景色
.active {
  background: rgba(255, 255, 255, 0.3);
  box-shadow: 0 1px 3px 0 rgba(0, 34, 77, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(8px);
  .container {
    color: #333;
    .more-icon {
      div {
        background: #000000;
      }
    }
  }
  .item {
    color: #333 !important;
  }
}

.item-inactive {
  position: relative;

  &::before {
    content: "";
    position: absolute;
    bottom: 10px;
    width: 0;
    height: 5px;
    background-color: #2ebc3c;
    opacity: 0.8;
    left: 50%;
    transition: all 0.2s;
    transform: translateX(-50%);
  }
  &:hover::before {
    width: 100%;
  }
}
.item-active {
  &::before {
    width: 100%;
  }
}
</style>
